﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using AutoUpdaterDotNET;

namespace POS
{
    public partial class MainProgram : DevExpress.XtraEditors.XtraForm
    {
        public static MainProgram _Main;
        public static Thread UpdateProcedures;

        //AutoUpdater upadter;

        public MainProgram()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            _Main = this;

            panelControl1.Controls.Add(new HomeScreen());
        }

        private void UpdateProcedures1()
        {
            timer1.Enabled = true;
            timer1.Start();

            FileSendRecieveClass.RecieveFiles recievefiles = new FileSendRecieveClass.RecieveFiles();
            recievefiles.StartListeningForFiles();
        }

        private void MainProgram_Load(object sender, EventArgs e)
        {
            StaticProperties.TimeToUpdate = .25;

            UpdateProcedures = new Thread(new ThreadStart(UpdateProcedures1));
            UpdateProcedures.Name = "UPDATE THREAD";
            timer1.Interval = Convert.ToInt32(TimeSpan.FromMinutes(StaticProperties.TimeToUpdate).TotalMilliseconds);
            UpdateProcedures.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            bool IsThereSqlConnection = DataTableClass.IsServerConnected();

            try
            {
                DataTableClass.SendDataToSqlServer(DataTableClass.Receipt.TableName);
                DataTableClass.SendDataToSqlServer(DataTableClass.EmployeeLogin.TableName);
                DataTableClass.SendDataToSqlServer(DataTableClass.EmployeeInfo.TableName);
                DataTableClass.SendDataToSqlServer(DataTableClass.EmployeeTimeSheet.TableName);
                DataTableClass.SendDataToSqlServer(DataTableClass.JobTitles.TableName);
                DataTableClass.SendDataToSqlServer(DataTableClass.MenuItemsCategory.TableName);
                DataTableClass.SendDataToSqlServer(DataTableClass.MenuItems.TableName);
                DataTableClass.SendDataToSqlServer(DataTableClass.ReceiptData.TableName);
                DataTableClass.SendDataToSqlServer(DataTableClass.PaymentTypes.TableName);

            }
            catch
            {
                DataTableClass.SaveDataSetToFile(DataTableClass.EmployeeLogin);
                DataTableClass.SaveDataSetToFile(DataTableClass.EmployeeInfo);
                DataTableClass.SaveDataSetToFile(DataTableClass.EmployeeTimeSheet);
                DataTableClass.SaveDataSetToFile(DataTableClass.JobTitles);
                DataTableClass.SaveDataSetToFile(DataTableClass.MenuItemsCategory);
                DataTableClass.SaveDataSetToFile(DataTableClass.MenuItems);
                DataTableClass.SaveDataSetToFile(DataTableClass.Receipt);
                DataTableClass.SaveDataSetToFile(DataTableClass.ReceiptData);
                DataTableClass.SaveDataSetToFile(DataTableClass.PaymentTypes);

                foreach (DataTable table in DataTableClass.POS.Tables)
                {
                    FileSendRecieveClass.SendFiles.SendFile(Environment.CurrentDirectory + @"\" + table.TableName + ".xml", table.TableName + ".xml");
                }
            }
            finally
            {
                if (IsThereSqlConnection)
                {
                    DataTableClass.EmployeeLogin.Clear();
                    DataTableClass.LoadDataTable("EmployeeLogin", DataTableClass.EmployeeLogin);
                    DataTableClass.EmployeeInfo.Clear();
                    DataTableClass.LoadDataTable("EmployeeInfo", DataTableClass.EmployeeInfo);
                    DataTableClass.EmployeeTimeSheet.Clear();
                    DataTableClass.LoadDataTable("EmployeeTimeSheet", DataTableClass.EmployeeTimeSheet);
                    DataTableClass.JobTitles.Clear();
                    DataTableClass.LoadDataTable("JobTitles", DataTableClass.JobTitles);
                    DataTableClass.MenuItemsCategory.Clear();
                    DataTableClass.LoadDataTable("MenuCategory", DataTableClass.MenuItemsCategory);
                    DataTableClass.LoadMenuCategoryDictionary();
                    DataTableClass.MenuItems.Clear();
                    DataTableClass.LoadDataTable("MenuItems", DataTableClass.MenuItems);
                    DataTableClass.LoadMenuItemsDictionary();
                    DataTableClass.Receipt.Clear();
                    DataTableClass.LoadDataTable("Receipt", DataTableClass.Receipt);
                    DataTableClass.ReceiptData.Clear();
                    DataTableClass.LoadDataTable("ReceiptData", DataTableClass.ReceiptData);
                    DataTableClass.PaymentTypes.Clear();
                    DataTableClass.LoadDataTable("PaymentTypes", DataTableClass.PaymentTypes);
                }
                else
                {
                    //DataTableClass.EmployeeLogin.Clear();
                    
                }
            }
        }
    }
}
